package com.yalu.dao.base.impl;

import java.sql.Connection;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Repository;

import com.yalu.dao.base.BaseJDBCDao;

/**
 * Dao实现类 - Dao实现类基类
 */

@Repository
public class BaseJDBCDaoImpl implements BaseJDBCDao {
	
	protected SessionFactory sessionFactory;
	protected JdbcTemplate jdbcTemplate;
	
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	protected Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	@Resource
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public int excuteUpdate(String sql) {
		return jdbcTemplate.update(sql);
	}
	
	public int[] excuteBatch( List<String> sqlList) {
		String[] sqlArr = sqlList.toArray(new String[0]); 
		return jdbcTemplate.batchUpdate(sqlArr);
	}
	
	public Connection getConnection(){
		Connection conn  = getSession().connection();
		return conn;
	}
	
	public SqlRowSet executeQuery(String sql){
		SqlRowSet rs =jdbcTemplate.queryForRowSet(sql);
		return rs;
	}


}